package com.eqdd.yiqidian.ui.mine.shenqing.fragment;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RefreshRecyclerViewFragmentCustom;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.BaseFragment;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.CarPaperListBean;
import com.eqdd.yiqidian.bean.rebuild.NoticeListResultBean;
import com.eqdd.yiqidian.bean.rebuild.ZhangPageResultBean;
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource;
import com.eqdd.yiqidian.helper.mvchelper.Rx2DataSource;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.JsonConverter;
import com.eqdd.yiqidian.ui.mine.pizhun.CarPaperInfoActivity;
import com.eqdd.yiqidian.ui.mine.shenqing.ReturnCarActivity;
import com.lzy.okgo.OkGo;
import com.lzy.okrx2.adapter.FlowableBody;
import com.shizhefei.mvc.MVCSwipeRefreshHelper;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;

/**
 * Created by lvzhihao on 17-7-19.
 */

public class MyCarFragment extends BaseFragment {
    RefreshRecyclerViewFragmentCustom dataBinding;
    private SlimAdapterEx<CarPaperListBean> slimAdapterEx;
    private int status;
    private ModelRx2DataSource<CarPaperListBean> dataSource;
    private MVCSwipeRefreshHelper<List<CarPaperListBean>> mvcHelper;
    private int pageNum = 0;

    @Override
    protected void setView() {
        if (slimAdapterEx == null) {
            dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_18, new SlimInjector<CarPaperListBean>() {
                @Override
                public void onInject(CarPaperListBean data, IViewInjector injector) {
                    injector.text(R.id.tv_title, data.getPlateNumber())
                            .text(R.id.tv_content, data.getStartTime() + "~" + data.getEndTime())
                            .text(R.id.bt_right, "归还车")
                            .visibility(R.id.bt_right, data.getStatus() == 2 ? View.VISIBLE : View.INVISIBLE)
                            .clicked(R.id.bt_right, view -> {
                                Bundle bundle = new Bundle();
                                bundle.putInt(Config.ID, data.getVehicleId());
                                ((BaseActivity) getActivity()).startActivityForResult(ReturnCarActivity.class, bundle, injector.pos());

                            });
                }
            })
                    .attachTo(dataBinding.recyclerView).updateData(new ArrayList());
            ItemClickSupport.addTo(dataBinding.recyclerView)
                    .setOnItemClickListener((recyclerView, position, v) -> {
                        Bundle bundle = new Bundle();
                        bundle.putInt(Config.ID, slimAdapterEx.getData().get(position).getId());
                        bundle.putInt(Config.STATUS, status);
                        bundle.putInt(Config.WHO, 0);
                        ((BaseActivity) getActivity()).startActivityForResult(CarPaperInfoActivity.class, bundle, position);

                    });
            dataSource = new ModelRx2DataSource<>(new ModelRx2DataSource.OnLoadSource() {
                @Override
                public Flowable<List> loadSource(int page, Rx2DataSource.DoneActionRegister register) {
                    pageNum = page == 1 ? 0 : pageNum;
                    return OkGo.<ZhangPageResultBean<List<CarPaperListBean>>>post(HttpConfig.BASE_URL + HttpConfig.MY_CAR_CREATE_LIST)
                            .params("page", pageNum)
                            .params("comid", AppConstent.user.getCompanyId())
                            .params("userGuid", AppConstent.user.getGuid())
                            .params("type", status)
                            .converter(new JsonConverter<ZhangPageResultBean<List<CarPaperListBean>>>() {
                                @Override
                                public void test() {
                                    super.test();
                                }
                            }).adapt(new FlowableBody<>())
                            .flatMap(noticeWraperResultBeanHttpResult -> {
                                pageNum = noticeWraperResultBeanHttpResult.getNextpage();
                                return Flowable.just(noticeWraperResultBeanHttpResult.getItems() == null ? new ArrayList<NoticeListResultBean>() : noticeWraperResultBeanHttpResult.getItems());
                            });

                }
            }, 10);
            mvcHelper = new MVCSwipeRefreshHelper<>(dataBinding.swipeRefresh);
            mvcHelper.setDataSource(dataSource);
            mvcHelper.setAdapter(slimAdapterEx);
            mvcHelper.refresh();
        }
    }

    @Override
    protected void initData() {

        status = getArguments().getInt(Config.STATUS);
    }


    @Override
    public ViewDataBinding initBinding(LayoutInflater inflater) {
        return dataBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_refresh_recycler_view, null, false);

    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Config.SUCCESS) {
            mvcHelper.refresh();
        }
    }
}
